﻿using System.Collections.Generic;
using System;

namespace PluginDB
{

    public class DBManager
    {
        #region Private Members
        //private static readonly ILog logger = LogManager.GetLogger("File");
        //private static readonly ILog log = LogManager.GetLogger(typeof(DBManager).Name);
        private readonly PluginDbEntities _pluginDbEntities;
        private readonly MoviesRepository _moviesRepository;
        private readonly TVShowsRepository _tvShowsRepository;
        private readonly CommonRepository _commonRepository;
        #endregion

        #region Ctor
        public DBManager()
        {
            try
            {
                _pluginDbEntities = new PluginDbEntities();

                _moviesRepository = new MoviesRepository(_pluginDbEntities);
                _tvShowsRepository = new TVShowsRepository(_pluginDbEntities);
                _commonRepository = new CommonRepository(_pluginDbEntities);

                //logger.Debug("DBManager loaded successfully");
            }
            catch (Exception ex)
            {
                //logger.Error("DBManager Load failed : " + ex.Message);
                throw new Exception(string.Format("DBManager :{0}", GetInnerException(ex)));
            }
        }

        private string GetInnerException(Exception ex)
        {
            if (ex.InnerException != null)
            {
                return GetInnerException(ex.InnerException);
            }

            return ex.Message;

        }
        #endregion

        public void AddVideo(PluginVideoInfo pluginVideoInfo)
        {
            if (pluginVideoInfo.IsTVShow)
            {
                this._tvShowsRepository.AddTVShow(pluginVideoInfo);
            }
            else
            {
                this._moviesRepository.AddMovie(pluginVideoInfo);
            }
        }

        public List<Category> GetCategoriesList(int page, int pageSize)
        {
            return _commonRepository.GetCategoriesList(page, pageSize);
        }

        public List<ValidVideosView> GetLatestMoviesList(int page, int pageSize)
        {
            return _moviesRepository.GetLatestVideosList(page, pageSize);
        }

        public List<ValidTVShowsView> GetLatestTVShowsList(int page, int pageSize)
        {
            return _tvShowsRepository.GetLatestVideosList(page, pageSize);
        }

        public List<ValidVideosView> GetHebSubMovies(int page, int pageSize)
        {
            return _moviesRepository.GetHebSubVideos(page, pageSize);
        }

        public List<ValidSeriesView> GetHebSubTVShows(int page, int pageSize)
        {
            return _tvShowsRepository.GetHebSubVideos(page, pageSize);
        }

        public bool IsMovieExists(string pluginUrl)
        {
            return _moviesRepository.IsMovieExists(pluginUrl);
        }

        public bool IsTVShowExists(string pluginUrl)
        {
            return _tvShowsRepository.IsTVShowExists(pluginUrl);
        }

        public bool IsSeriesExists(string seriesName)
        {
            return _tvShowsRepository.IsSeriesExists(seriesName);
        }

        public List<Site> GetSitesNames(int page, int pageSize)
        {
            return _commonRepository.GetSitesNames(page, pageSize);
        }

        public List<ValidVideosView> GetMoviesByCategoryId(long categoryId, int page, int pageSize)
        {
            return _moviesRepository.GetMoviesByCategoryId(categoryId, page, pageSize);
        }

        public List<ValidTVShowsView> GetTVShowsByCategoryId(long categoryId, int page, int pageSize)
        {
            return _tvShowsRepository.GetTVShowsByCategoryId(categoryId, page, pageSize);
        }

        public List<ValidVideosView> GetHebDubMovies(int page, int pageSize)
        {
            return _moviesRepository.GetHebDubVideos(page, pageSize);
        }

        public List<ValidVideosView> GetAtoZMoviesList(string startWord, bool isHebWord, int page, int pageSize)
        {
            return _moviesRepository.GetAtoZMoviesList(startWord, isHebWord, page, pageSize);
        }

        public List<ValidSeriesView> GetAtoZSeriesList(string startWord, bool isHebWord, int page, int pageSize)
        {
            return _tvShowsRepository.GetAtoZSeriesList(startWord, isHebWord, page, pageSize);
        }

        public List<ValidVideosView> GetMoviesBySite(long siteId, int page, int pageSize)
        {
            return _moviesRepository.GetMoviesBySite(siteId, page, pageSize);
        }

        public List<ValidSeriesView> GetTVShowsBySite(long siteId, int page, int pageSize)
        {
            return _tvShowsRepository.GetTVShowsBySite(siteId, page, pageSize);
        }

        public Video GetMovieByID(long videoId)
        {
            return _moviesRepository.GetMovieByID(videoId);
        }

        public TVShow GetTVShowByID(long videoId)
        {
            return _tvShowsRepository.GetTVShowByID(videoId);
        }

        public VideosLink GetVideoLinkByID(long linkId)
        {
            return _moviesRepository.GetVideoLinkByID(linkId);
        }

        public TVShowsLink GetTVShowLinkByID(long linkId)
        {
            return _tvShowsRepository.GetTVShowLinkByID(linkId);
        }

        public List<ValidSeriesView> GetSeriesList(int page, int pageSize)
        {
            return _tvShowsRepository.GetSeriesList(page, pageSize);
        }

        public List<ValidTVShowsView> GetTVShowsBySeries(long seriesId, int page, int pageSize)
        {
            return _tvShowsRepository.GetTVShowsBySeries(seriesId, page, pageSize);
        }

        public List<long> GetHebSubSeasonsbySeriesId(long seriesId, int page, int pageSize)
        {
            return _tvShowsRepository.GetHebSubSeasonsbySeriesId(seriesId, page, pageSize);
        }

        public List<long> GetSeasonsbySeriesAndSiteId(long siteId, long seriesId, int page, int pageSize)
        {
            return _tvShowsRepository.GetSeasonsbySeriesAndSiteId(siteId, seriesId, page, pageSize);
        }

        public List<long> GetSeasonsbySeriesId(long seriesId, int page, int pageSize)
        {
            return _tvShowsRepository.GetSeasonsbySeriesId(seriesId, page, pageSize);
        }

        public List<ValidTVShowsView> GetHebSubTVShowsBySeasonId(long seriesId, long seasonNumber, int page, int pageSize)
        {
            return _tvShowsRepository.GetHebSubTVShowsBySeasonId(seriesId, seasonNumber, page, pageSize);
        }

        public List<ValidTVShowsView> GetTVShowsBySeasonAndSiteId(long siteId, long seriesId, long seasonNumber, int page, int pageSize)
        {
            return _tvShowsRepository.GetTVShowsBySeasonAndSiteId(siteId, seriesId, seasonNumber, page, pageSize);
        }

        public List<ValidTVShowsView> GetTVShowsBySeasonId(long seriesId, long seasonNumber, int page, int pageSize)
        {
            return _tvShowsRepository.GetTVShowsBySeasonId(seriesId, seasonNumber, page, pageSize);
        }

        public FileHostingCooky GetCookieCode(string sourceName)
        {
            return _commonRepository.GetCookieCode(sourceName);
        }

        public void InsertNewCookieCode(string cookieCode, DateTime expiredDate, string sourceName)
        {
            _commonRepository.InsertNewCookieCode(cookieCode, expiredDate, sourceName);
        }

        public List<ValidVideosView> GetMoviesBySearchText(string searchText, int page, int pageSize)
        {
            return _moviesRepository.GetMoviesBySearchText(searchText, page, pageSize);
        }

        public List<ValidSeriesView> GetTVShowsBySearchText(string searchText, int page, int pageSize)
        {
            return _tvShowsRepository.GetTVShowsBySearchText(searchText, page, pageSize);
        }

        public List<VideosLink> GetAllVideosLinks(bool getOnlyInvalid)
        {
            return _moviesRepository.GetAllVideosLinks(getOnlyInvalid);
        }

        public List<TVShowsLink> GetAllTVShowsLinks()
        {
            return _tvShowsRepository.GetAllTVShowsLinks();
        }

        public void SaveAllVideos()
        {
            _moviesRepository.SaveAllVideos();
        }

        public void SaveAllTVShows()
        {
            _tvShowsRepository.SaveAllTVShows();
        }

        public List<TVShow> GetAllTVShows()
        {
            return _tvShowsRepository.GetAllTVShows();
        }

        public List<Video> GetAllVideos()
        {
            return _moviesRepository.GetAllVideos();
        }

        public List<Series> GetAllSeries()
        {
            return _tvShowsRepository.GetAllSeries();
        }

        public List<Series> GetSeriesByName(string seriesName)
        {
            return _tvShowsRepository.GetSeriesByName(seriesName);
        }

        public void DeleteSeries(Series series)
        {
            _tvShowsRepository.DeleteSeries(series);
        }

        public void SaveAllSeries()
        {
            _tvShowsRepository.SaveAllSeries();
        }

        public List<TVShow> GetTVShowsBySeriesId(long seriesId)
        {
            return _tvShowsRepository.GetTVShowsBySeriesId(seriesId);
        }

        public Series GetSeriesById(long seriesId)
        {
            return _tvShowsRepository.GetSeriesById(seriesId);
        }

        public List<Video> GetMoviesByName(string videoName)
        {
            return _moviesRepository.GetMoviesByName(videoName);
        }

        public List<VideosLink> GetMovieLinksByVideoID(long videoId)
        {
            return _moviesRepository.GetMovieLinksByVideoID(videoId);
        }

        public void DeleteVideo(Video movie)
        {
            _moviesRepository.DeleteVideo(movie);
        }

        public void DeleteVideoLink(VideosLink videosLink)
        {
            _moviesRepository.DeleteVideoLink(videosLink);
        }

        public bool IsUserExists(long userId)
        {
            return _commonRepository.IsUserExists(userId);
        }

        public bool IsUserExists(string userName, string password)
        {
            return _commonRepository.IsUserExists(userName, password);
        }

        public bool IsUserNameExists(string userName)
        {
            return _commonRepository.IsUserNameExists(userName);
        }

        public bool IsUserExists(string userUniqeGuid)
        {
            return _commonRepository.IsUserExists(userUniqeGuid);
        }

        public User GetUser(long userId)
        {
            return _commonRepository.GetUser(userId);
        }

        public User GetUser(string userName, string password)
        {
            return _commonRepository.GetUser(userName, password);
        }

        public User GetUser(string userGuid)
        {
            return _commonRepository.GetUser(userGuid);
        }

        public void RemoveSeriesForUser(long userId, long seriesId)
        {
            var series = GetSeries(seriesId);
            _commonRepository.RemoveSeriesForUser(userId, series);
        }

        public User CreateUser(string userName, string password)
        {
            return _commonRepository.CreateUser(userName, password);
        }

        public List<Series> GetSeriesByUserId(long userId)
        {
            return _tvShowsRepository.GetSeriesByUserId(userId);
        }

        public bool IsUserSeriesExists(long userId, long seriesId)
        {
            return _tvShowsRepository.IsUserSeriesExists(userId, seriesId);
        }

        public void AddSeriesToUser(long userId, Series series)
        {
            _commonRepository.AddSeriesToUser(userId, series);
        }

        public bool IsSeriesExists(long seriesId)
        {
            return _tvShowsRepository.GetSeriesById(seriesId) != null;
        }

        public Series GetSeries(long seriesId)
        {
            return _tvShowsRepository.GetSeriesById(seriesId);
        }

        public List<ValidSeriesView> GetSeriesBySeriesId(long seriesId)
        {
            return _tvShowsRepository.GetSeriesBySeriesId(seriesId);
        }

        public bool IsUserVideoExists(long userId, long videoId)
        {
            return _moviesRepository.IsUserVideoExists(userId, videoId);
        }

        public void AddVideoToUser(long userId, Video video)
        {
            _commonRepository.AddVideoToUser(userId, video);
        }

        public void RemoveVideoForUser(long userId, Video video)
        {
            _commonRepository.RemoveVideoForUser(userId, video);
        }

        public long GetSeriesIdFromVideoId(int videoId)
        {
            return _tvShowsRepository.GetSeriesIdByVideoId(videoId);
        }

        public List<Video> GetMovieLinksByUserId(long userId)
        {
            return _moviesRepository.GetVideosByUserId(userId);
        }

        public List<ValidVideosView> GetVideosByVideoId(long videoId)
        {
            return _moviesRepository.GetVideosByVideoId(videoId);
        }

        public List<Video> GetAllVideosBySite(long siteId)
        {
            return _moviesRepository.GetAllVideosBySite(siteId);
        }

        public List<Job> GetSiteScrapperJobs()
        {
            return _commonRepository.GetSiteScrapperJobs();
        }

        public void UpdateJobTypeRunTime(long jobTypeID, DateTime runTime)
        {
            _commonRepository.UpdateJobTypeRunTime(jobTypeID, runTime);
        }

        public void UpdateJobRunTime(long jobId, DateTime runTime)
        {
            _commonRepository.UpdateJobRunTime(jobId, runTime);
        }

        public List<JobsType> GetJobsTypesList()
        {
            return _commonRepository.GetJobsTypesList();
        }

        public Job GetJob(string jobName)
        {
            return _commonRepository.GetJob(jobName);
        }

        public Job CreateJob(string jobName, string jobTypeName)
        {
            return _commonRepository.CreateJob(jobName, jobTypeName);
        }

        public JobsType GetJobType(string jobName)
        {
            return _commonRepository.GetJobType(jobName);    
        }

        public JobsType CreateJobType(string jobName, long runtimeIntervalInHours)
        {
            return _commonRepository.CreateJobType(jobName, runtimeIntervalInHours);
        }

        public bool ActivateUser(string userGuid)
        {
            return _commonRepository.ActivateUser(userGuid);
        }

        public bool IsSiteExists(string siteName)
        {
            return _commonRepository.IsSiteExists(siteName);
        }

        public int GetVideosCount()
        {
            return _moviesRepository.GetVideosCount();
        }

        public int GetSeriesCount()
        {
            return _tvShowsRepository.GetSeriesCount();
        }

        public int GetUserVideosCount(long userID)
        {
            return _moviesRepository.GetUserVideosCount(userID);
        }

        public int GetUserSeriesCount(long userID)
        {
            return _tvShowsRepository.GetUserSeriesCount(userID);
        }

        public List<VideosLink> GetVideosLinksBySourceID(long sourceId)
        {
            return _moviesRepository.GetVideosLinksBySourceID(sourceId);
        }

        public List<TVShowsLink> GetTVShowsLinksBySourceID(long sourceId)
        {
            return _tvShowsRepository.GetTVShowsLinksBySourceID(sourceId);
        }

        public void DeleteTVShow(TVShow tVShow)
        {
            _tvShowsRepository.DeleteTVShow(tVShow);
        }

        public void DeleteTVShowLink(TVShowsLink videoLink)
        {
            _tvShowsRepository.DeleteTVShowLink(videoLink);
        }
    }
}
